import os
import time

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import logging
from selenium.webdriver.common.by import By

from config import BASE_DIR


class Base():
    def __init__(self,driver):
        logging.debug("正在初始化driver对象: {}".format(driver))
        self.driver = driver

    def getUrl(self,url):
        self.driver.get(url)

    #查找元素
    def base_find_element(self,ele):
        try:
            logging.info(f"查找元素:{ele}")
            element = WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located(ele))
            return element
        except:
            print(f"未找到元素{ele}")
            logging.error(f"未找到元素{ele}")
            return False

    def maxWindows(self):
        logging.info(f"窗口最大化")
        self.driver.maximize_windwo()

    #元素等待
    def element_wait(self,ele,secs=10):
        logging.info(f"等待元素:{ele}")
        WebDriverWait(self.driver,secs,1).until(EC.presence_of_element_located(ele),'元素不存在')

    #等待元素消失
    def element_wait_disappear(self,ele,secs=10):
        logging.info(f"等待元素消失:{ele}")
        WebDriverWait(self.driver,secs).until_not(EC.presence_of_element_located(ele),"元素不存在")


    #判断元素是否可见
    def element_is_displayed(self,ele):
        try:
            self.base_find_element(ele)
            logging.info(f"元素:{ele}可见")
            return True
        except:
            logging.info(f"元素:{ele}不可见")
            return False

    def Enter(self,ele):
        logging.info(f"回车")
        self.driver.base_find_element(ele).send_keys(Keys.ENTER)

    #单击
    def element_click(self,ele):
        logging.info(f"点击元素:{ele}")
        self.base_find_element(ele).click()

    #Js方式点击
    def element_JsClick(self,ele):
        el = self.base_find_element(ele)
        logging.info(f"点击元素:{ele}")
        self.driver.execute_script("arguments[0].click();", el)

    #鼠标点击
    def element_MouseClick(self,ele):
        el = self.base_find_element(ele)
        logging.info(f"点击元素:{ele}")
        ActionChains(self.driver).move_to_element(el).click().perform()

    #元素文本输入
    def element_input(self,ele,value):
        logging.info(f"输入文本:{value}")
        self.base_find_element(ele).send_keys(value)

    #清空输入框
    def clear(self,ele):
        logging.info(f"清空输入框:{ele}")
        self.driver.base_find_element(ele).clear()

    #获取元素文本内容
    def get_value(self,ele):
        return self.driver.base_find_element(ele).text

    #获取元素属性值
    def get_attribute(self,ele,attribute):
        return self.driver.base_find_element(ele).get_attribute(attribute)

    #获取窗口标题
    def get_windowTitle(self):
        logging.info(f"获取窗口标题")
        return self.driver.title


    #Alert弹窗-确定
    def alertAccept(self):
        logging.info(f"弹窗-确定")
        self.driver.switch_to.alert.accept()

    # Alert弹窗-取消
    def alertDismiss(self):
        logging.info(f"弹窗-取消")
        self.driver.switch_to.alert.dismiss()


    #通过标题切换窗口
    def swich_windows_byTitle(self,title):
        handles = self.driver.window_handles
        for handle in handles:
            self.driver.switch_to_window(handle)
            if title == self.driver.title:
                logging.info(f"切换到窗口:{title}")
                break

    #切换到最新打开的窗口
    def swich_newWindows(self):
        handles = self.driver.window_handles
        self.driver.switch_to_window(handles[-1])
        logging.info(f"进入新打开的窗口")

    #打开新的标签页并导航到目标地址
    def newTab_Navigate(self,url):
        js = "window.open('{}','_blank');"
        self.driver.execute_script(js.format(url))
        logging.info(f"导航到:{url}")

    # 切换frame
    def switch_frame(self,ele):
        self.driver.switch_to.frame(self.driver.base_find_element(ele))
        logging.info(f"进入frame:{ele}")

    # 切换回到主窗口
    def switch_default_content(self):
        self.driver.switch_to.default_content()
        logging.info(f"切换回到主窗口")

    #执行指定JS代码
    def js(self,jsScript):
        self.driver.execute_script(jsScript)
        logging.info(f"执行指定JS代码{jsScript}")

    def get_screenshot(self):
        logging.info(f"调用截图方法")
        time.sleep(2)
        img_path = BASE_DIR + os.sep + "screenshot" + os.sep + "{}.png".format(time.strftime("%Y_%m_%d_%H_%M_%S"))
        self.driver.get_screenshot_as_file(img_path)
